﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;

namespace EFong.View.Documentation
{
    public partial class Frm_NewInsurancePolicy : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_NewInsurancePolicy()
        {
            InitializeComponent();
            this.cmb_BankAccount.ReadOnly(true);
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
                {
                    this.save();
                };
            this.cmb_PaymentType.SelectedIndexChanged += (s, e) =>
            {
                this.cmb_BankAccount.SetValue(default(Contract.BankAccount));
                this.cmb_BankAccount.ReadOnly(this.cmb_PaymentType.SelectedIndex <= 0);
            };
        }

        protected override void LoadDatas()
        {
            this.cmb_Vendor.Properties.BindingData(_masterProxy.GetAllInsuranceVendor().ReturnValue);
            this.cmb_BankAccount.Properties.BindingData(_masterProxy.GetAllEffectivedBankAccount());
        }

        private void save()
        {
            List<Contract.InsurancePolicy> newInsurancePolicys = null;

            UIOperManager.SaveData(
                () =>
                {
                    if (string.IsNullOrEmpty(txt_Start.GetText()))
                    {
                        MsgBox.Info("请输入开始单号.");
                        return false;
                    }

                    if (string.IsNullOrEmpty(txt_End.GetText()))
                    {
                        MsgBox.Info("请输入结束单号.");
                        return false;
                    }

                    return true;
                },
                () =>
                {
                    newInsurancePolicys = getInsurancePolicys();

                    Contract.NewInsurancePolicy cNewInsurancePolicy = new Contract.NewInsurancePolicy();
                    cNewInsurancePolicy.InsurancePolicyList = newInsurancePolicys;
                    cNewInsurancePolicy.BankAccount = cmb_BankAccount.GetValue() as Contract.BankAccount;
                    cNewInsurancePolicy.PaymentType = cmb_PaymentType.GetValue() as string;

                    return _masterProxy.NewInsurancePolicy(cNewInsurancePolicy).IsSuccess;
                },
                MsgResource.Save_Complete,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private List<Contract.InsurancePolicy> getInsurancePolicys()
        {
            List<Contract.InsurancePolicy> newInsurancePolicys = new List<Contract.InsurancePolicy>();

            List<string> refs = DocumentHelper.GetRefList(txt_Start.GetText(), txt_End.GetText());


            Contract.Currency cnyCurrency = _baseProxy.GetCNYCurrency().ReturnValue;

            if (refs != null && refs.Count > 0)
            {
                foreach (var eachRef in refs)
                {
                    newInsurancePolicys.Add(new Contract.InsurancePolicy
                    {
                        Type = txt_Type.GetText(),
                        BasePrice = spe_BasePrice.GetValue(),
                        ListPrice = spe_Price.GetValue(),
                        Ref = eachRef,
                        Currency = cnyCurrency,
                        Vendor = cmb_Vendor.GetValue() as Contract.RVendor
                    });
                }
            }

            return newInsurancePolicys;
        }
    }
}
